﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Compiler
{
    /// <summary>
    /// 符号表
    /// </summary>
    public class SymbolTable
    {
        /// <summary>
        /// 分隔符
        /// </summary>
        private static readonly char[] Separator = new char[] { ' ' };
        /// <summary>
        /// 操作符
        /// </summary>
        public static List<string> Operators = new List<string>("+ - * / < > = == != <= >= ".Split(Separator, StringSplitOptions.RemoveEmptyEntries));
        /// <summary>
        /// 边界符
        /// </summary>
        public static List<string> Boundarys = new List<string>(" { } ( ) [ ] ; , #".Split(Separator, StringSplitOptions.RemoveEmptyEntries));
        /// <summary>
        /// 关键字
        /// </summary>
        public static List<string> Keywords = new List<string>("if else while for int float char bool return private piblic void break continue".Split(Separator, StringSplitOptions.RemoveEmptyEntries));
        /// <summary>
        /// 标识符
        /// </summary>
        public static List<string> Identifiers { get; set; } = new List<string>();
        /// <summary>
        /// 常数
        /// </summary>
        public static List<string> Numbers { get; set; } = new List<string>();
        /// <summary>
        /// 初始化
        /// </summary>
        public static void Init()
        {
            Identifiers.Clear();
            Numbers.Clear();
        }
        /// <summary>
        /// 判断是否为操作符
        /// </summary>
        public static bool IsOperator(string op)
        {
            return Operators.Contains(op);
        }
        /// <summary>
        /// 判断是否为边界符
        /// </summary>
        public static bool IsBoundary(string op)
        {
            return Boundarys.Contains(op);
        }
        /// <summary>
        /// 判断是否为关键字
        /// </summary>
        public static bool IsKeyword(string identifier)
        {
            return Keywords.Contains(identifier);
        }
        /// <summary>
        /// 记录新出现的标识符
        /// </summary>
        public static void RecordIdentifier(string identifier)
        {
            //如果现有标识符表中没有当前标识符，则记录到标识符表中
            if (!Identifiers.Contains(identifier))
            {
                Identifiers.Add(identifier);
            }
        }

    }
}
